//
//  STCustomTabBar.swift
//  Demo-04-TabBarTest
//
//  Created by 苏统世 on 16/5/11.
//  Copyright © 2016年 SuTon. All rights reserved.
//

import UIKit

class STCustomTabBar: UITabBar {

    var centerButtonBlock:(()->())?
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let itemW=self.frame.width/5.0
        var index = 0
        for view in self.subviews{
            if view.isKindOfClass(NSClassFromString("UITabBarButton")!){
//            if view.isKindOfClass(UITabBarButton.class) {
                view.frame = CGRect(x: CGFloat(index) * itemW, y: 0, width: itemW, height: self.frame.height)
                index++
                if index==2{
                    index++
                }
            }
            centerButton.frame = CGRect(x: 2 * itemW, y: 0, width: itemW, height: self.frame.height)
        }
    }
    // MARK: - 按钮点击事件
    func didClickCenterButton(button: UIButton) {
        // 最终需要去Modal出控制器,View不能Modal出控制器,只有控制器才能Modal出控制器,当按钮点击的时候需要让控制器去做事情
        // 3.调用闭包
        centerButtonBlock?()
    }
    // MARK: - 懒加载
    lazy var centerButton: UIButton = {
        // 创建按钮
        let button = UIButton(type: UIButtonType.Custom)
        // 设置按钮
        // 背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 按钮的图片 tabbar_compose_icon_add_highlighted
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "didClickCenterButton:", forControlEvents: UIControlEvents.TouchUpInside)
        self.addSubview(button)
        return button
    }()
    
    
    
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */

}
